package com.unittec.zk.provider.auth.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unittec.zk.provider.auth.entity.SysDeployFormEntity;
import com.unittec.zk.provider.auth.entity.SysFormEntity;
import com.unittec.zk.provider.auth.service.SysDeployFormService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.auth.domain.SysDepartDto;
import com.unittec.zk.sdk.auth.domain.SysDeployFormDto;
import com.unittec.zk.sdk.auth.domain.SysFormDto;
import com.unittec.zk.sdk.auth.request.*;
import com.unittec.zk.sdk.auth.response.*;
import com.unittec.zk.sdk.flowable.domain.FlowProcDefDto;
import com.unittec.zk.sdk.flowable.request.Req666001;
import com.unittec.zk.sdk.flowable.request.Req666005;
import com.unittec.zk.sdk.flowable.response.Res666001;
import com.unittec.zk.sdk.flowable.response.Res666005;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.root.enums.ErrorEnum;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangzheng
 * @description 系统发布表单controller
 * 2021/12/10
 */
@RestController
@Api(tags = "系统发布表单")
public class SysDeployFormController {
    @Autowired
    Log log;

    @Autowired
    SysDeployFormService sysDeployFormService;

    @RequestMapping(value = "/101661", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过表单编码选择系统发布表单", notes = "通过表单编码选择系统发布表单")
    public Result<Res101661> selectSysDeployFormById(@RequestBody GatewayRequest<Req101661> req){
        log.debug(req, "selectSysDeployFormById  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101661 res101661 = null;
        try {
            Req101661 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101661 = new Res101661();
            SysDeployFormEntity entity = sysDeployFormService.selectSysDeployFormById(content.getId());
            SysDeployFormDto deployForm = new SysDeployFormDto();
            if(entity != null){
                BeanUtils.copyProperties(entity,deployForm);
            }
            res101661.setDeployForm(deployForm);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "selectSysDeployFormById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101661);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101662", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过发布编码选择系统发布表单", notes = "通过发布编码选择系统发布表单")
    public  Result<Res101662> selectSysDeployFormByDeployId(@RequestBody GatewayRequest<Req101662> req){
        log.debug(req, "selectSysDeployFormByDeployId  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101662 res101662 = null;
        try {
            Req101662 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101662 = new Res101662();
            SysFormDto entity = sysDeployFormService.selectSysDeployFormByDeployId(content.getDeployId());
            res101662.setObj(entity);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "selectSysDeployFormByDeployId {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101662);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101663", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "通过表单对象选择系统发布表单", notes = "通过表单对象选择系统发布表单")
    public Result<Res101663> selectSysDeployFormList(@RequestBody GatewayRequest<Req101663> req){
        log.debug(req, "selectSysDeployFormList  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101663 res101663 = null;
        try {
            Req101663 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res101663 = new Res101663();
            SysDeployFormEntity exportSysDeployForm = new SysDeployFormEntity();

            SysDeployFormDto sysDeployForm = content.getSysDeployForm();

            if (sysDeployForm != null) {
                BeanUtils.copyProperties(sysDeployForm, exportSysDeployForm);
            }
            List<SysDeployFormEntity> sysDeployFormEntities = sysDeployFormService.selectSysDeployFormList(exportSysDeployForm);
            List<SysDeployFormDto> list = new ArrayList<>();
            for(SysDeployFormEntity deployForm : sysDeployFormEntities){
                SysDeployFormDto exportDeployForm = new SysDeployFormDto();
                if(deployForm != null){
                    BeanUtils.copyProperties(deployForm, exportDeployForm);
                }
                list.add(exportDeployForm);
            }
            res101663.setList(list);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "selectSysDeployFormList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101663);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101664", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "插入系统发布表单", notes = "插入系统发布表单")
    public Result<Res101664> insertSysDeployForm(@RequestBody GatewayRequest<Req101664> req){
        log.debug(req, "save  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101664 res101664 = null;
        InputStream in = null;
        try {
            res101664 = new Res101664();
            Req101664 content = req.getContent();
            SysDeployFormEntity exportSysDeployForm = new SysDeployFormEntity();
            SysDeployFormDto sysDeployForm = content.getSysDeployForm();
            if (sysDeployForm != null) {
                BeanUtils.copyProperties(sysDeployForm, exportSysDeployForm);
            }
            sysDeployFormService.insertSysDeployForm(exportSysDeployForm);
            res101664.setContent("导入表单成功");
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "save {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101664);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101665", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新系统发布表单", notes = "更新系统发布表单")
    public Result<Res101665> updateSysDeployForm(@RequestBody GatewayRequest<Req101665> req){
        log.debug(req, "updateSysDeployForm  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101665 res101665 = null;
        InputStream in = null;
        try {
            res101665 = new Res101665();
            Req101665 content = req.getContent();
            SysDeployFormEntity exportSysDeployForm = new SysDeployFormEntity();
            SysDeployFormDto sysDeployForm = content.getSysDeployForm();
            if (sysDeployForm != null) {
                BeanUtils.copyProperties(sysDeployForm, exportSysDeployForm);
            }
            sysDeployFormService.updateSysDeployForm(exportSysDeployForm);
            res101665.setContent("更新表单成功");
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "updateSysDeployForm {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101665);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101666", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "批量删除系统发布表单", notes = "批量删除系统发布表单")
    public Result<Res101666> deleteSysDeployFormByIds(@RequestBody GatewayRequest<Req101666> req){
        log.debug(req, "deleteSysDeployFormByIds  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101666 res101666 = null;
        InputStream in = null;
        try {
            res101666 = new Res101666();
            Req101666 content = req.getContent();
            sysDeployFormService.deleteSysDeployFormByIds(content.getIds());
            res101666.setContent("批量删除表单成功");
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "deleteSysDeployFormByIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101666);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/101667", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除系统发布表单", notes = "删除系统发布表单")
    //int Long id
    public Result<Res101667>  deleteSysDeployFormById(@RequestBody GatewayRequest<Req101667> req){
        log.debug(req, "deleteSysDeployFormById  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res101667 res101667 = null;
        InputStream in = null;
        try {
            res101667 = new Res101667();
            Req101667 content = req.getContent();
            int mark = sysDeployFormService.deleteSysDeployFormById(content.getId());
            res101667.setContent("删除表单成功");
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "deleteSysDeployFormById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res101667);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

}
